package gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import controller.CPR;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class BajaProveedor extends javax.swing.JFrame {

	private static final long serialVersionUID = 1L;
	private JLabel lblTitle;
	private JTextField txtCodigo;
	private JTextField txtNroCuit;
	private JLabel lblNroCuit;
	private JLabel lblCodigo;
	private JButton cmdAceptar;
	private JButton cmdVolver;
	private static BajaProveedor instance;
	
	private BajaProveedor() {
		super();
		initGUI();
		this.setLocationRelativeTo(null);
	}
	
	public static BajaProveedor getInstance()
	{
		if (instance == null)
			instance = new BajaProveedor();
		
		return instance;
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
			{
				lblTitle = new JLabel();
				getContentPane().add(lblTitle);
				lblTitle.setText("CPR - Proveedores - Eliminar Proveedor");
				lblTitle.setFont(new java.awt.Font("Calibri",0,20));
				lblTitle.setBounds(12, 12, 373, 32);
			}
			{
				cmdVolver = new JButton();
				getContentPane().add(cmdVolver);
				cmdVolver.setText("Volver");
				cmdVolver.setBounds(24, 319, 91, 31);
				cmdVolver.setFont(new java.awt.Font("Calibri",0,16));
				cmdVolver.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						cmdVolverActionPerformed(evt);
					}
				});
			}
			{
				cmdAceptar = new JButton();
				getContentPane().add(cmdAceptar);
				getContentPane().add(getLblRazSoc());
				getContentPane().add(getLblNroCuit());
				getContentPane().add(getTxtRazSoc());
				getContentPane().add(getTxtNroCuit());
				cmdAceptar.setText("Aceptar");
				cmdAceptar.setBounds(483, 319, 91, 31);
				cmdAceptar.setFont(new java.awt.Font("Calibri",0,16));
				cmdAceptar.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent evt) {
							cmdAceptarActionPerformed(evt);
					}				
				});
			}
			pack();
			setSize(600, 400);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private JLabel getLblRazSoc() {
		if(lblCodigo == null) {
			lblCodigo = new JLabel();
			lblCodigo.setText("Codigo");
			lblCodigo.setBounds(217, 139, 49, 14);
			lblCodigo.setFont(new java.awt.Font("Calibri",0,14));
		}
		return lblCodigo;
	}
	
	private JLabel getLblNroCuit() {
		if(lblNroCuit == null) {
			lblNroCuit = new JLabel();
			lblNroCuit.setText("Nro Cuit");
			lblNroCuit.setBounds(212, 176, 54, 14);
			lblNroCuit.setFont(new java.awt.Font("Calibri",0,14));
		}
		return lblNroCuit;
	}
	
	private JTextField getTxtRazSoc() {
		if(txtCodigo == null) {
			txtCodigo = new JTextField();
			txtCodigo.setBounds(290, 136, 90, 20);
			txtCodigo.setFont(new java.awt.Font("Calibri",0,14));
		}
		return txtCodigo;
	}
	
	private JTextField getTxtNroCuit() {
		if(txtNroCuit == null) {
			txtNroCuit = new JTextField();
			txtNroCuit.setBounds(290, 173, 90, 20);
			txtNroCuit.setFont(new java.awt.Font("Calibri",0,14));
		}
		return txtNroCuit;
	}
	
	//Acciones
	//
	private void cmdVolverActionPerformed(ActionEvent evt) {
		this.dispose();
	}
	
	private void cmdAceptarActionPerformed(ActionEvent evt)
	{
		boolean delete = false;
		if (! txtCodigo.getText().equalsIgnoreCase("") || ! txtNroCuit.getText().equalsIgnoreCase(""))
		{
			try{
				
				int codigo = 0;
				if (txtCodigo.getText().length() > 0)
					codigo = Integer.parseInt(txtCodigo.getText());
				
				String nroCuit = txtNroCuit.getText();	
				delete = CPR.getInstance().bajaProveedor(codigo, nroCuit);
			}catch(Exception e)
			{
				JOptionPane.showMessageDialog(null, "Verifique los campos ingreasdos", "Error", JOptionPane.ERROR_MESSAGE);
				return;
			}			
		}
		else{
			JOptionPane.showMessageDialog(null, "Debe llenar alguno de los campos", "Error", JOptionPane.ERROR_MESSAGE);
			return;
		}
		
		if (!delete)
			JOptionPane.showMessageDialog(null, "No existe el proveedor", "Error", JOptionPane.ERROR_MESSAGE);	
		else{
			JOptionPane.showMessageDialog(null, "Proveedor eliminado con Exito", "Confirm", JOptionPane.DEFAULT_OPTION);	
			txtNroCuit.setText("");
			txtCodigo.setText("");
		}
	}
}
